﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Threading;
using System.Diagnostics;

namespace server.app
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        static string OrmMapping(object obj)
        {
            var type = obj.GetType();
            var sb = new StringBuilder();
            foreach (var p in type.GetProperties())
            {
                var atts = p.GetCustomAttributes(typeof(comm.objst.OrmAttr), true);
                sb.Append("public const string ");
                if (atts.Length > 0)
                {
                    var att = (comm.objst.OrmAttr)atts[0];
                    if (att.IsKey)
                    {
                        sb.Append("KEY_");
                    }
                    else if (att.BigText)
                    {
                        sb.Append("BIG_TEXT_");
                    }
                }
                sb.Append(comm.StringHelper.LineSplit(p.Name).ToUpper() + "=\"" + p.Name + "\";\r\n");
            }
            return sb.ToString();
        }

        const int CONSOLE_MAX = 100;

        World _world = null;

        public MainWindow()
        {
            InitializeComponent();
            this.LoadConfig1();

            comm.Console.OnWriteLine = ConsoleWriteLine;
            Console.OnWriteLine = ConsoleWriteLine;

            _world = new World();

            #region loop
            comm.ThreadHelper.Run(() =>
            {
                while (true)
                {
                    Dispatcher.BeginInvoke(new Action(() =>
                    {
                        tbInfo.Text =
                            "ObjectManager FPS:" + _world.ObjectManager.FPS + "\r\n" +
                            "PlayerManager FPS:" + _world.PlayerManager.FPS + " \r\n" +
                            "Stars (" + _world.StarManager.AllStarCount + " db:" + _world.DataManager.TableStar.AllStarCount + ")\r\n" +
                            "Spaces (online:" + _world.SpaceManager.AllSpaceCount + " db:" + _world.DataManager.TableStar.AllSpaceCount + ")\r\n" +
                            "Players (online:" + _world.PlayerManager.AllOnlineCount + " db:" + _world.DataManager.TablePlayer.AllPlayerCount + ")\r\n" +
                            "Units (db:" + _world.DataManager.TableUnit.AllUnitCount + ")\r\n" +
                            "Items (db:" + _world.DataManager.TableItem.AllItemCount + ")\r\n" +
                            "";
                    }));
                    Thread.Sleep(1000);
                }
            });
            #endregion

            btnInitTables.Click += new RoutedEventHandler(btnInitTables_Click);
            btnCodeReplace.Click += new RoutedEventHandler(btnCodeReplace_Click);
        }

        void btnCodeReplace_Click(object sender, RoutedEventArgs e)
        {
            btnCodeReplace.IsEnabled = false;
            var funcDic = new Dictionary<comm.CodeFillType, Func<object, string>>();
            funcDic.Add(comm.CodeFillType.ORM_CONST, OrmMapping);
            funcDic.Add(comm.CodeFillType.DEFINE_FILE_CONST, obj =>
            {
                return _world.DefineFile.ExportConst();
            });
            comm.CodeFill.ReplaceAll(funcDic);
            btnCodeReplace.IsEnabled = true;
            Console.WriteLine("code replace complete!");
        }
        void btnInitTables_Click(object sender, RoutedEventArgs e)
        {
            btnInitTables.IsEnabled = false;
            _world.DataManager.InitAllTable();
            btnInitTables.IsEnabled = true;
            Console.WriteLine("init tables complete!");
        }

        void ConsoleWriteLine(object obj, comm.ConsoleColor color, StackFrame stackFrame)
        {
            var colorDic = new Dictionary<comm.ConsoleColor, Color>();
            colorDic.Add(comm.ConsoleColor.NORMAL, Color2.White(50));
            colorDic.Add(comm.ConsoleColor.RED, Color2.Red());
            colorDic.Add(comm.ConsoleColor.YELLOW, Color2.Yellow());

            Dispatcher.BeginInvoke(new Action(() =>
            {
                var fontColor = colorDic[color];
                var txt = obj == null ? "null" : obj.ToString();
                var time = DateTime.Now;
                var grid = new Grid();

                #region tbTxt
                var tbTxt = new TextBox
                {
                    IsReadOnly = true,
                    BorderThickness = new Thickness(0),
                    Background = null,
                    FontSize = 12,
                    TextWrapping = TextWrapping.Wrap,
                    Foreground = new SolidColorBrush(fontColor),
                    Text = txt,
                    Margin = new Thickness(20, 0, 0, 0)
                };
                grid.Children.Add(tbTxt);
                #endregion

                #region btnExpand
                var tbPlus = new TextBlock
                {
                    Text = "+",
                    Foreground = new SolidColorBrush(Color2.White(50)),
                    HorizontalAlignment = HorizontalAlignment.Center,
                    VerticalAlignment = VerticalAlignment.Top
                };
                var btnExpand = new Border
                {
                    Width = 18,
                    Height = 18,
                    Child = tbPlus,
                    BorderBrush = new SolidColorBrush(Color2.White(20)),
                    BorderThickness = new Thickness(1),
                    CornerRadius = new CornerRadius(4),
                    Background = new SolidColorBrush(Color2.White(10)),
                    VerticalAlignment = VerticalAlignment.Top,
                    HorizontalAlignment = HorizontalAlignment.Left
                };
                btnExpand.MouseLeftButtonDown += new MouseButtonEventHandler((o, e) =>
                {
                    if (tbPlus.Text == "+")
                    {
                        tbTxt.Text = "Time:" + time + "\r\n" +
                            "File:" + stackFrame.GetFileName() + "\r\n" +
                            "Line:" + stackFrame.GetFileLineNumber() + "\r\n" +
                            "Column:" + stackFrame.GetFileColumnNumber() + "\r\n" +
                            "-----------------------\r\n" +
                             txt;
                        tbTxt.Foreground = new SolidColorBrush(Color2.White(30));
                        tbPlus.Text = "-";
                    }
                    else
                    {
                        tbTxt.Text = txt;
                        tbTxt.Foreground = new SolidColorBrush(fontColor);
                        tbPlus.Text = "+";
                    }
                });
                grid.Children.Add(btnExpand);
                #endregion

                spConsole.Children.Add(grid);

                if (spConsole.Children.Count > CONSOLE_MAX)
                {
                    spConsole.Children.RemoveAt(0);
                }

                svConsole.ScrollToBottom();
            }));
        }
    }
}
